package com.exam.service.Impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.exam.entity.Teacher;
import com.exam.mapper.TeacherMapper;
import com.exam.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TeacherServiceImpl implements TeacherService {
    @Autowired
    private TeacherMapper teacherMapper;

    // 查询所有教师
    public IPage<Teacher> findAll(Page<Teacher> page) {
        return teacherMapper.findAll(page);
    }

    // 查询所有教师
    public List<Teacher> findAll() {
        return teacherMapper.findAll();
    }

    // 根据id查询教师
    public Teacher findById(Integer teacherId) {
        return teacherMapper.findById(teacherId);
    }

    // 根据id删除教师
    public int deleteById(Integer teacherId) {
        return teacherMapper.deleteById(teacherId);
    }

    // 更新教师信息
    public int update(Teacher teacher) {
        return teacherMapper.update(teacher);
    }

    // 添加教师信息
    public Teacher add(Teacher teacher) {
        //根据老师的个人信息查询
        List<Teacher> teacherList = teacherMapper.findByTelAndCardIdAndEmail(teacher);
        if(teacherList != null  && teacherList.size() > 0){
            return null;
        }else {
            teacherMapper.add(teacher);
            teacherList = teacherMapper.findByAllTeacherData(teacher);
            return teacherList.get(0);
        }
    }

    // 重置密码
    @Override
    public int editPassword(Teacher teacher) {

        String pwd = teacher.getPwd();
        List<Teacher> teacherList = teacherMapper.findByAllTeacherData(teacher);
        if (teacherList != null  && teacherList.size() > 0 ) {
            teacher.setPwd(pwd);
            teacherMapper.update(teacher);
            return 200;
        }else{
            return 500;
        }
    }
}
